Restore IntegralRC integral-correction clamp (deployed-LoopKit safety bound)#36
Closed
ps2 wants to merge 1 commit into
Closed
Restore IntegralRC integral-correction clamp (deployed-LoopKit safety bound)#36ps2 wants to merge 1 commit into
ps2 wants to merge 1 commit into
Conversation
… bound) The port of Loop's algorithm from LoopKit to the standalone LoopAlgorithm package dropped IntegralRetrospectiveCorrection's integral-correction limits — a safety guardrail that bounds the wound-up integral term so it cannot drive the forecast into over- or under-dosing. This restores that clamp, faithful to deployed LoopKit (LoopKit/RetrospectiveCorrection/IntegralRetrospectiveCorrection.swift): zeroTempEffect = |ISF x basalRate| positiveLimit = min(max(glucose - rangeMax, 1x zeroTempEffect), 4x zeroTempEffect) negativeLimit = -max(10, glucose - rangeMin) integralCorrection = clamp(integralCorrection, negativeLimit, positiveLimit) The clamp needs ISF, scheduled basal rate, and correction range at the decision time. computeEffect() and generatePrediction() gain these as OPTIONAL inputs (nil => legacy unclamped behavior, so the public API stays backward-compatible). LoopAlgorithm.run() passes input.target, so the deployed dosing path is clamped; pure prediction (which carries no correction range) stays unclamped. Tests (IntegralRetrospectiveCorrectionTests): a wound-up integral is pinned to the positive/negative limits, and left untouched when within limits. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CkvtagxJNmFrbbHTgd9CDX
Collaborator
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The port of Loop's algorithm from LoopKit to the standalone LoopAlgorithm package dropped
IntegralRetrospectiveCorrection's integral-correction limits — a safety guardrail that bounds the wound-up integral term so it cannot drive the forecast into over- or under-dosing. This PR restores that clamp.It is a faithful restoration of the deployed-LoopKit formula (
LoopKit/RetrospectiveCorrection/IntegralRetrospectiveCorrection.swift):zeroTempEffect, larger the further BG is above the correction-range top (more room to correct a real high).Design — backward compatible
The clamp needs ISF, scheduled basal rate, and correction range at the decision time. These are added as optional inputs to
computeEffect(...)andgeneratePrediction(...):nil⇒ legacy unclamped behavior, so the public API is unchanged for existing callers.LoopAlgorithm.run()passesinput.target, so the deployed dosing path is clamped by default.LoopPredictionInputcarries no correction range) stays unclamped.StandardRetrospectiveCorrectionaccepts and ignores the inputs (no integral term to bound).Tests
New cases in
IntegralRetrospectiveCorrectionTests(readingintegralCorrectiondirectly):All 141 package tests pass.